/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.controller;

import com.alibaba.fastjson.JSONObject;
import com.xlx.openLink.base.BaseDateMessage;
import com.xlx.openLink.base.ThreadRunTimeExcption;
import com.xlx.openLink.conf.MailBean;
import com.xlx.openLink.enums.ResultCodeEnum;
import com.xlx.openLink.enums.SysParamsEnums;
import com.xlx.openLink.service.BackService;
import com.xlx.openLink.service.SysService;
import com.xlx.openLink.vo.MysqlInfo;
import com.xlx.openLink.vo.RequestSBackList;
import com.xlx.openLink.vo.RequestSystLst;
import com.xlx.openLink.vo.SshVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Slf4j
@Controller
@RequestMapping("/sys")
public class SysParamController {

    @Autowired
    private SysService sysService;

    @PostMapping("/listByPage")
    @ResponseBody
    public BaseDateMessage listByPage(@RequestBody RequestSystLst requestSystLst) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(sysService.listByPage(requestSystLst));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }

    @GetMapping("/selectByCode")
    @ResponseBody
    public BaseDateMessage listByPage(@RequestParam(value = "code") String code) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            if (code.equalsIgnoreCase(SysParamsEnums.SYS_SSH.getCode()) && sysService.selctByCode(code).getSysParam() != null) {
                baseDateMessage.setData(JSONObject.parseObject(sysService.selctByCode(code).getSysParam(), SshVo.class));
            } else if (code.equalsIgnoreCase(SysParamsEnums.MYSQL_INFO.getCode()) && sysService.selctByCode(code).getSysParam() != null) {
                baseDateMessage.setData(JSONObject.parseObject(sysService.selctByCode(code).getSysParam(), MysqlInfo.class));
            } else if (code.equalsIgnoreCase(SysParamsEnums.SYS_MAIL.getCode()) && sysService.selctByCode(code).getSysParam() != null) {
                baseDateMessage.setData(JSONObject.parseObject(sysService.selctByCode(code).getSysParam(), MailBean.class));
            }
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }

}
